using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Monster : Role
{

    public void Updata(GameManager gameManager)
    {
        Vector2 tNextPos = pos;
        switch (eRun)
        {
            case ERun.none:
                eRun = ChangeMove();
                break;
            case ERun.up:
                tNextPos.y += (image.rectTransform.rect.height + 1);
                break;
            case ERun.down:
                tNextPos.y -= 1;
                break;
            case ERun.left:
                tNextPos.x -= 1;
                break;
            case ERun.right:
                tNextPos.x += (image.rectTransform.rect.width + 1);
                break;
            case ERun.death:
                break;
        }
        if (gameManager.map.IsHit(tNextPos, gameManager.role))
        {
            eRun = ERun.none;
        }
        else
        {
            tNextPos = pos;
            switch (eRun)
            {
                case ERun.up:
                    tNextPos.y += 1;
                    break;
                case ERun.down:
                    tNextPos.y -= 1;
                    break;
                case ERun.left:
                    tNextPos.x -= 1;
                    break;
                case ERun.right:
                    tNextPos.x += 1;
                    break;
            }
            if (!gameManager.map.IsHit(tNextPos, gameManager.role))
            {
                pos = tNextPos;
            }
        }
        image.rectTransform.localPosition = new Vector2(pos.x + image.rectTransform.rect.width / 2f, pos.y + image.rectTransform.rect.height / 2f);

        UpdateMonsterAni(gameManager);
    }
    void UpdateMonsterAni(GameManager gameManager)
    {
        runtime += Time.deltaTime;

        float tRoleRunTime = runtime * speed;
        switch (eRun)
        {
            case ERun.none:

                break;
            case ERun.up:
                int tFrameUp = (int)tRoleRunTime % gameManager.upTextureList.Count;
                image.texture = gameManager.upTextureList[tFrameUp];
                break;
            case ERun.down:
                int tFrameDown = (int)tRoleRunTime % gameManager.downTextureList.Count;
                image.texture = gameManager.downTextureList[tFrameDown];
                break;
            case ERun.left:
                int tFrameLeft = (int)tRoleRunTime % gameManager.leftTextureList.Count;
                image.texture = gameManager.leftTextureList[tFrameLeft];
                break;
            case ERun.right:
                int tFrameRight = (int)tRoleRunTime % gameManager.rightTextureList.Count;
                image.texture = gameManager.rightTextureList[tFrameRight];
                break;
            default:
                break;
        }


    }

    ERun ChangeMove()
    {
        int i = Random.Range(0, 4);
        return (ERun)(i + 1);
    }

}
